package com.example.springbootdemo.consumer;

import com.example.springbootdemo.constant.RabbitmqConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  rabbitmq消费者
 * </p>
 *
 * @author yurenwei
 * @since 2023/9/6
 */
@Slf4j
@Component
public class RabbitmqConsumer {

    @RabbitListener(queuesToDeclare = @Queue(RabbitmqConstant.TEST_QUEUE_NAME))
    public void process(String msg, Channel channel, Message message) {
        log.info("【接收消息】{}【当前时间】{}",msg, LocalDateTime.now());
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
